import {getUserInfo, loginByUsername} from "@/api/login";
import {getToken, removeDeadline, removeInfo, removeToken, setDeadline, setInfo, setMenu, setToken} from "@/utils/auth";

const user = {
  state: {
    user: {},
    status: "",
    code: "",
    token: getToken(),
    id: 0,
    name: "",
    avatar: "",
    introduction: "",
    roles: [],
    menus: [], // 菜单权限
    // 按钮级权限管控 - ①新增 button 全局变量
    buttons: [], // 按钮权限
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_USER: (state, user) => {
      state.user = user;
    },
    SET_CODE: (state, code) => {
      state.code = code;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction;
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting;
    },
    SET_STATUS: (state, status) => {
      state.status = status;
    },
    SET_ID: (state, id) => {
      state.id = id;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus;
    },
    // 按钮级权限管控 - ②新增 mutations 操作 button
    SET_BUTTONS: (state, buttons) => {
      state.buttons = buttons;
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({commit}, userInfo) {
      return new Promise((resolve, reject) => {
        loginByUsername(userInfo)
          .then(response => {
            const data = response.data;
            const deadline = new Date().getTime() + 9 * 3600 * 1000;

            setDeadline(deadline);
            commit("SET_TOKEN", data.token); // 将token保存到cookie里 -> 作为前端用户已登录的标识
            setToken(response.data.token);
            setMenu(JSON.stringify(response.data.menus));
            setInfo(response.data.info);
            resolve();
          })
          // .then(async () => {
          //   const res = await getAdminMenu();
          //   setMenu(JSON.stringify(res.data));
          // })
          .catch(error => {
            // 登录失败，回传提示信息
            reject(error);
          });
      });
    },

    // 前端 登出
    FedLogOut({commit}) {
      return new Promise(resolve => {
        commit("SET_TOKEN", "");
        removeToken();
        removeInfo();
        removeDeadline();
        console.log("退出");
        resolve();
      });
    },

    // 动态修改权限
    ChangeRoles({commit, dispatch}, role) {
      return new Promise(resolve => {
        commit("SET_TOKEN", role);
        setToken(role);
        getUserInfo(role).then(response => {
          const data = response.data;
          commit("SET_ROLES", data.roles);
          commit("SET_MENUS", data.menus);
          commit("SET_BUTTONS", data.buttons);

          commit("SET_ID", data.id);
          commit("SET_NAME", data.name);
          commit("SET_AVATAR", data.avatar);
          commit("SET_INTRODUCTION", data.introduction);
          resolve();
        });
      });
    }
  }
};

export default user;
